// Comments are added at several places to original function. // This function partitions array elements to the left // and right of pivot. It is used by quick sort function. size_t partition(int * arr, int l, int r) { int i = l + 1, j = r; // Left item is selected as pivot. int pivot = arr[l]; while (i <= j) { while (pivot < arr[j]) j--; if (j == l) return l; while (i < r && pivot > arr[i]) i++; if (i == j) break; // Swapped to strictly put items to the left and right of pivot. if (i < j) swap(&arr[i], &arr[j]); } swap(&arr[l], &arr[j]); return j; }